package com.anjubao.parking.municipal.parkinglot.service;

import com.anjubao.parking.municipal.common.global.BusinessCodeEnum;
import com.anjubao.parking.municipal.common.utils.MunicipalParkingUtils;
import com.anjubao.parking.municipal.common.utils.U;
import com.anjubao.parking.municipal.parkinglot.constant.DeviceOperationStatusEnum;
import com.anjubao.parking.municipal.parkinglot.constant.DeviceStatusEnum;
import com.anjubao.parking.municipal.parkinglot.constant.DeviceTypeEnum;
import com.anjubao.parking.municipal.parkinglot.dto.PDADto;
import com.anjubao.parking.municipal.parkinglot.dto.ParkingLotDto;
import com.anjubao.parking.municipal.parkinglot.feign.ParkingLotFeignClient;
import com.anjubao.parking.municipal.parkinglot.form.PDAQueryForm;
import com.anjubao.parking.municipal.parkinglot.mapper.ParkingLotDevicePdaMapper;
import com.anjubao.parking.municipal.parkinglot.mapper.ParkingLotDeviceStatusMapper;
import com.anjubao.parking.municipal.parkinglot.model.ParkingLotDevicePda;
import com.anjubao.parking.municipal.parkinglot.model.ParkingLotDeviceStatus;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDateTime;


/**
 * PDA手持机 service层
 *
 */
@Slf4j
@Service
public class ParkingLotDevicePdaService extends ServiceImpl<ParkingLotDevicePdaMapper, ParkingLotDevicePda> {

    @Resource
    private ParkingLotDevicePdaMapper parkingLotDevicePdaMapper;

    @Autowired
    private ParkingLotFeignClient parkingLotFeignClient;

    @Resource
    private ParkingLotDeviceStatusMapper parkingLotDeviceStatusMapper;

    @Transactional(rollbackFor = Exception.class)
    public ParkingLotDevicePda saveParkingLotDevicePdaService(ParkingLotDevicePda parkingLotDevicePda){
        parkingLotDevicePda.setParkingLotId(MunicipalParkingUtils.getIdByCode(parkingLotDevicePda.getParkingLotCode()));
        ParkingLotDto parkingLotDto=parkingLotFeignClient.getParkingLot(parkingLotDevicePda.getParkingLotCode()).getResult();
        parkingLotDevicePda.setParkingLotProvinceId(parkingLotDto.getProvinceId()).setParkingLotProvinceName(parkingLotDto.getProvinceName())
                .setParkingLotCityId(parkingLotDto.getCityId()).setParkingLotCityName(parkingLotDto.getCityName())
                .setParkingLotDistrictId(parkingLotDto.getDistrictId()).setParkingLotDistrictName(parkingLotDto.getDistrictName());
        if(StringUtils.isEmpty(parkingLotDevicePda.getCode())){//判断是否有code
            parkingLotDevicePda.setId(U.id());
            parkingLotDevicePda.setCode(MunicipalParkingUtils.generateCode(parkingLotDevicePda.getId(),100));
            saveOrUpdate(parkingLotDevicePda);
            ParkingLotDeviceStatus parkingLotDeviceStatus=convertDeviceStatusEntity(parkingLotDevicePda,parkingLotDto);
            parkingLotDeviceStatus.setId(U.id());
            parkingLotDeviceStatus.setCode(MunicipalParkingUtils.generateCode(parkingLotDeviceStatus.getId(), BusinessCodeEnum.DEVICE_STATUS.getCode()));
            parkingLotDeviceStatusMapper.insert(parkingLotDeviceStatus);
        }else{
            parkingLotDevicePda.setId(MunicipalParkingUtils.getIdByCode(parkingLotDevicePda.getCode()));
            saveOrUpdate(parkingLotDevicePda);
            ParkingLotDeviceStatus parkingLotDeviceStatus=convertDeviceStatusEntity(parkingLotDevicePda,parkingLotDto);
            parkingLotDeviceStatusMapper.update(parkingLotDeviceStatus, new UpdateWrapper<ParkingLotDeviceStatus>().eq("device_id", parkingLotDevicePda.getId()));
        }
        return parkingLotDevicePda;
    }

    private ParkingLotDeviceStatus convertDeviceStatusEntity(ParkingLotDevicePda pda, ParkingLotDto parkingLot){
        ParkingLotDeviceStatus parkingLotDeviceStatus = new ParkingLotDeviceStatus();
        //todo
//        parkingLotDeviceStatus.setParkingLotSlotId(videoPile.getParkingLotSlotId());
//        parkingLotDeviceStatus.setParkingLotSlotCode(videoPile.getParkingLotSlotCode());
//        parkingLotDeviceStatus.setParkingLotSlotName(videoPile.getParkingLotSlotName());
        parkingLotDeviceStatus.setDeviceId(pda.getId());
        parkingLotDeviceStatus.setDeviceCode(pda.getCode());
        parkingLotDeviceStatus.setDeviceName(pda.getName());
        parkingLotDeviceStatus.setDeviceType(DeviceTypeEnum.VIDEO_PILE.getCode());
        parkingLotDeviceStatus.setIsOnline(DeviceOperationStatusEnum.OnLine.getCode());
        parkingLotDeviceStatus.setStatus(DeviceStatusEnum.Normal.getCode());
        parkingLotDeviceStatus.setProvinceId(pda.getParkingLotProvinceId());
        parkingLotDeviceStatus.setProvinceName(pda.getParkingLotProvinceName());
        parkingLotDeviceStatus.setCityId(pda.getParkingLotCityId());
        parkingLotDeviceStatus.setCityName(pda.getParkingLotCityName());
        parkingLotDeviceStatus.setDistrictId(pda.getParkingLotDistrictId());
        parkingLotDeviceStatus.setDistrictName(pda.getParkingLotDistrictName());
        parkingLotDeviceStatus.setLocation(parkingLot.getLocation());
        parkingLotDeviceStatus.setNote(pda.getNote());
//        parkingLotDeviceStatus.setFixBy(null);
//        parkingLotDeviceStatus.setFixTime(null);
//        parkingLotDeviceStatus.setFixNote(null);
        parkingLotDeviceStatus.setLastReportTime(LocalDateTime.now());
        return parkingLotDeviceStatus;
    }
    public IPage<PDADto> getPDAs(Page<PDADto> page, PDAQueryForm form) {
        return parkingLotDevicePdaMapper.getPDAs(page,form);
    }

    public ParkingLotDevicePda getPDA(long id) {
        return parkingLotDevicePdaMapper.selectById(id);
    }

    public void deletePDA(long id) {
        parkingLotDevicePdaMapper.deleteById(id);
    }
}
